﻿namespace FlexField.Metadata
{
    /// <summary>
    /// Service to enable localization for application facing metadata errors.
    /// </summary>
    /// <remarks>
    ///These errors are returned to controllers and are generally used as display messages to end users.
    /// </remarks>
    public class MetadataErrorDescriber
    {
        /// <summary>
        /// Returns the default <see cref="MetadataError"/>.
        /// </summary>
        /// <returns>The default <see cref="MetadataError"/>.</returns>
        public virtual MetadataError DefaultError()
        {
            return new MetadataError
            {
                Code = nameof(DefaultError),
                Description = Resources.DefaultError
            };
        }

        /// <summary>
        /// Returns an <see cref="MetadataError"/> indicating a concurrency failure.
        /// </summary>
        /// <returns>An <see cref="MetadataError"/> indicating a concurrency failure.</returns>
        public virtual MetadataError ConcurrencyFailure()
        {
            return new MetadataError
            {
                Code = nameof(ConcurrencyFailure),
                Description = Resources.ConcurrencyFailure
            };
        }

        /// <summary>
        /// Returns an <see cref="MetadataError"/> indicating the specified field <paramref name="fieldName"/> is invalid.
        /// </summary>
        /// <param name="fieldName">The field name that is invalid.</param>
        /// <returns>An <see cref="MetadataError"/> indicating the specified field <paramref name="fieldName"/> is invalid.</returns>
        public virtual MetadataError InvalidFieldName(string fieldName)
        {
            return new MetadataError
            {
                Code = nameof(InvalidFieldName),
                Description = string.Format(Resources.Culture, Resources.InvalidFieldName, fieldName)
            };
        }

        /// <summary>
        /// Returns an <see cref="MetadataError"/> indicating the specified <paramref name="fieldName"/> already exists.
        /// </summary>
        /// <param name="fieldName">The field name that already exists.</param>
        /// <returns>An <see cref="MetadataError"/> indicating the specified <paramref name="fieldName"/> already exists.</returns>
        public virtual MetadataError DuplicateFieldName(string fieldName)
        {
            return new MetadataError
            {
                Code = nameof(DuplicateFieldName),
                Description = string.Format(Resources.Culture, Resources.DuplicateFieldName, fieldName)
            };
        }

        /// <summary>
        /// Returns an <see cref="MetadataError"/> indicating the specified <paramref name="typeName"/> is invalid.
        /// </summary>
        /// <param name="typeName">The invalid type name.</param>
        /// <returns>An <see cref="MetadataError"/> indicating the specific <paramref name="typeName"/> is invalid.</returns>
        public virtual MetadataError InvalidTypeName(string typeName)
        {
            return new MetadataError
            {
                Code = nameof(InvalidTypeName),
                Description = string.Format(Resources.Culture, Resources.InvalidTypeName, typeName)
            };
        }

        /// <summary>
        /// Returns an <see cref="MetadataError"/> indicating the specified <paramref name="typeName"/> already exists.
        /// </summary>
        /// <param name="typeName">The duplicate type.</param>
        /// <returns>An <see cref="MetadataError"/> indicating the specific <paramref name="typeName"/> already exists.</returns>
        public virtual MetadataError DuplicateTypeName(string typeName)
        {
            return new MetadataError
            {
                Code = nameof(DuplicateTypeName),
                Description = string.Format(Resources.Culture, Resources.DuplicateTypeName, typeName)
            };
        }

        /// <summary>
        /// Returns an <see cref="MetadataError"/> indicating the specified <paramref name="type"/> is not found.
        /// </summary>
        /// <param name="type">The type to look up.</param>
        /// <returns>An <see cref="MetadataError"/> indicating the specific <paramref name="type"/> is not found.</returns>
        public virtual MetadataError TypeNotFound(string type)
        {
            return new MetadataError
            {
                Code = nameof(TypeNotFound),
                Description = string.Format(Resources.Culture, Resources.TypeNotFound, type)
            };
        }

        /// <summary>
        /// Returns an <see cref="MetadataError"/> indicating the specified <paramref name="field"/> is not found .
        /// </summary>
        /// <param name="type">The type of the field belongs to.</param>
        /// <param name="field">The field to look up.</param>
        /// <returns>An <see cref="MetadataError"/> indicating the specific <paramref name="field"/> is not found.</returns>
        public virtual MetadataError FieldNotFound(string type, string field)
        {
            return new MetadataError
            {
                Code = nameof(TypeNotFound),
                Description = string.Format(Resources.Culture, Resources.FieldNotFound, type, field)
            };
        }
    }
}
